package com.lzs.bootsafety.business.sys.controller;

import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.sys.entity.SysPost;
import com.lzs.bootsafety.business.sys.service.SysPostService;
import com.lzs.bootsafety.common.base.controller.BaseController;
import com.lzs.bootsafety.common.util.Resu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/sys/post")
public class PostController extends BaseController {
    @Autowired
    private SysPostService sysPostService;
    @GetMapping("/getPosts")
    public Resu getPosts(){
        return Resu.suces().put("data",sysPostService.getPostList());
    }
    @GetMapping("/list")
    @RequiresPermissions("sys:post:list")
    public Resu list(SysPost sysPost){
        PageInfo<SysPost> sysPostPageInfo = sysPostService.getPostByPage(sysPost);
        return Resu.page(sysPostPageInfo.getList(),sysPostPageInfo.getTotal(),200);
    }
    @PostMapping("/add")
    @RequiresPermissions("sys:post:add")
    public Resu add(@RequestBody SysPost sysPost){
        sysPost.setCreater(getUserName());
        sysPost.setCreateTime(new Date());
        return respAdd(sysPostService.addSelective(sysPost));
    }
    @GetMapping("/info")
    @RequiresPermissions("sys:post:info")
    public Resu info(Long postId){
        SysPost sysPost = sysPostService.getByPrimaryKey(postId);
        return Resu.suces().put("data",sysPost);
    }
    @PutMapping("/modify")
    @RequiresPermissions("sys:post:modify")
    public Resu modify(@RequestBody SysPost sysPost){
        sysPost.setMendifier(getUserName());
        sysPost.setModifyTime(new Date());
        return respudp(sysPostService.updateSysPost(sysPost));
    }
    @DeleteMapping("/del")
    @RequiresPermissions("sys:post:del")
    public Resu del(Long[] postIds){
        int r = sysPostService.batchDel(postIds);
        return respDel(r);
    }

}
